using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace PdfLib
{
    /// <summary>
    /// structure to hold text on a single line of pdf page
    /// </summary>
    [Serializable]
    public class PdfTextLine : IComparable
    {
        public List<PdfTextToken> TextTokens;

        public RectangleF Rect;

        public int CompareTo(object obj)
        {
            if (obj.GetType() == typeof(PdfTextLine))
            {
                PdfTextLine otherLine = (PdfTextLine)obj;
                if (Math.Abs(this.Rect.Y - otherLine.Rect.Y) < 4)
                    return 0;
                else
                    return this.Rect.Y.CompareTo(otherLine.Rect.Y);
            }
            else
                throw new Exception("Invalid type to compare");
        }

        public static PdfTextLine Empty
        {
            get
            {
                PdfTextLine emptyLine = new PdfTextLine();
                emptyLine.TextTokens = new List<PdfTextToken>();
                emptyLine.Rect = new RectangleF();
                return emptyLine;
            }
        }

        public static bool IsEmpty(PdfTextLine line)
        {
            if (line.TextTokens == null || line.TextTokens.Count == 0)
                return true;
            else
                return false;
        }

        public string LineContent
        {
            get
            {
                if(this.TextTokens !=null && this.TextTokens.Count>0)
                {
                    StringBuilder lineContent = new StringBuilder();
                    for(int i=0;i<this.TextTokens.Count;i++)
                    {
                        lineContent.Append(this.TextTokens[i].Text);
                        if(i<this.TextTokens.Count -1)
                        {
                            lineContent.Append(" ");
                        }
                    }
                    return lineContent.ToString();
                }
                else
                {
                    return null;
                }
            }
        }

        public PdfTextLine()
        {
            this.TextTokens = new List<PdfTextToken>();
            this.Rect = new RectangleF();
        }
    }
}
